package com.itboy.mnt.database.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import com.itboy.mnt.database.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mnt.database.bo.MntDatabaseAddBo;
import com.itboy.mnt.database.bo.MntDatabaseQueryBo;
import com.itboy.mnt.database.bo.MntDatabaseEditBo;
import com.itboy.mnt.database.domain.MntDatabase;
import com.itboy.mnt.database.mapper.MntDatabaseMapper;
import com.itboy.mnt.database.vo.MntDatabaseVo;
import com.itboy.mnt.database.service.IMntDatabaseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据库管理Service业务层处理
 *
 * @author itboy
 * @date 2021-07-29
 */
@Slf4j
@Service
public class MntDatabaseServiceImpl extends ServicePlusImpl<MntDatabaseMapper, MntDatabase> implements IMntDatabaseService {

    @Override
    public MntDatabaseVo queryById(Long dbId){
        return getVoById(dbId, MntDatabaseVo.class);
    }

    @Override
    public TableDataInfo<MntDatabaseVo> queryPageList(MntDatabaseQueryBo bo) {
        PagePlus<MntDatabase, MntDatabaseVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MntDatabaseVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MntDatabaseVo> queryList(MntDatabaseQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MntDatabaseVo.class);
    }

    private LambdaQueryWrapper<MntDatabase> buildQueryWrapper(MntDatabaseQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MntDatabase> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), MntDatabase::getName, bo.getName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MntDatabaseAddBo bo) {
        MntDatabase add = BeanUtil.toBean(bo, MntDatabase.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MntDatabaseEditBo bo) {
        MntDatabase update = BeanUtil.toBean(bo, MntDatabase.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MntDatabase entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public boolean testConnection(MntDatabase mntDatabase) {
		try {
			return SqlUtils.testConnection(mntDatabase.getJdbcUrl(), mntDatabase.getUserName(), mntDatabase.getPwd());
		} catch (Exception e) {
			log.error(e.getMessage());
			return false;
		}
	}
}
